package com.xxmw.transaction.common.utils.http;

import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.TreeMap;

/**
 *
 * @since  2020/2/5 16:19
 * @author luowenliang
 */
public class RestTemplateUtil {

    private static final RestTemplate restTemplate = new RestTemplate();

    public static String get(String url) {
        return get(url,String.class);
    }

    public static <T> T get(String url, Class<T> responseType) {
        return restTemplate.getForObject(url, responseType);
    }

    public static ResponseEntity<String> getForEntity (String url) {
        return getForEntity(url, String.class);
    }

    public static <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType) {
        HttpHeaders headers = new HttpHeaders();
        //解决访问403问题
        headers.add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36");
        HttpEntity<Resource> httpEntity = new HttpEntity<Resource>(headers);
        return restTemplate.exchange(url, HttpMethod.GET, httpEntity, responseType);
    }

    public static <T> ResponseEntity<T> getForEntity(String url,HttpHeaders headers, Class<T> responseType) {
        //解决访问403问题
        headers.add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36");
        HttpEntity<Resource> httpEntity = new HttpEntity<Resource>(headers);
        return restTemplate.exchange(url, HttpMethod.GET, httpEntity, responseType);
    }

    public static String post(String url, String body) {
        return post(url, body, String.class);
    }

    public static String post(String url, String body, MediaType header) {
        return post(url, body, header, String.class);
    }

    public static <T> T post(String url, String body, Class<T> responseType) {
        return post(url, body, MediaType.APPLICATION_FORM_URLENCODED, responseType);
    }

    public static <T> T post(String url, String body, MediaType header, Class<T> responseType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(header);
        HttpEntity<String> entity = new HttpEntity<>(body, headers);
        return restTemplate.postForObject(url, entity, responseType);
    }

    public static <T> T postForObject(String url, HttpEntity httpEntity, Class<T> responseType) {
        return restTemplate.postForObject(url, httpEntity, responseType);
    }

    public static <T> T deleteForObject(String url, HttpEntity httpEntity, Class<T> responseType) {
        return restTemplate.exchange(url, HttpMethod.DELETE, httpEntity, responseType).getBody();
    }


    public static void main(String[] args) {
//        ResponseEntity<String> entity = getForEntity("https://shouduanxin.com/zh-cn/");
        //按ASCII码从小到大排序
        Map<String, String> treeMap = new TreeMap<String, String>((o1, o2) -> o1.compareTo(o2));
        treeMap.put("service", "get_tdcode");
        treeMap.put("third_order_id", "123456789");
        treeMap.put("t0", "t0");
        treeMap.put("jspay_flag", "0");

        StringBuilder sb = new StringBuilder();
        treeMap.entrySet().forEach(entry -> sb.append(entry.toString()).append("&"));

        System.out.println(sb.substring(0, sb.length() - 1));

        String res = post("https://log.study.163.com/api/v1/log.do", sb.toString());

        System.out.println(res);
    }
}
